<template>
	<view class="home_wrap">
		<view class="head_wrap">
			<view :style="'height:' + statusBarHeight + 'px'"></view>
			<view class="title" :style="'height:' + navigatorBarHeight + 'px'">首页</view>
			<view class="search_item" @click="focusinput()">
				<image :src="remoteUrl + 'search_icon.png'" class="search_icon" mode=""></image>
				<input type="text" placeholder="搜索" disabled="true" @focus="focusinput" />
			</view>
		</view>
		<swiper :autoplay="true" class="swiper_wrap" circular="true">
			<swiper-item v-for="(item,index) in banner_list">
				<image :src="ossUrl + item.image" class="banner_img" mode=""></image>
			</swiper-item>
		</swiper>
		<view class="flex-row equal-division">
			<view class="flex-col items-center group_4 group_16"
				@click="jumpLink('/pages/connections/connections',false)">
				<image class="image_7" :src="remoteUrl + 'connections_img.png'" />
				<text class="font_2 mt-6">人脉</text>
			</view>
			<view class="flex-col items-center group_4 equal-division-item_2"
				@click="jumpLink('/pages/hall/hall',false)">
				<image class="image_7" :src="remoteUrl + 'hall_img.png'" />
				<text class="font_2 mt-6">供需大厅</text>
			</view>
			<view class="flex-col items-center group_4 group_15"
				@click="jumpLink('/pages/pages-list/forum_page/forum_page',true)">
				<image class="image_7" :src="remoteUrl + 'forum_img.png'" />
				<text class="font_2 mt-6">官方发布</text>
			</view>
			<view class="flex-col items-center group_4 equal-division-item_2"
				@click="jumpLink('/pages/coures/coures',false)">
				<image class="image_7" :src="remoteUrl + 'course_img.png'" />
				<text class="font_2 mt-6">大咖课程</text>
			</view>
		</view>
		<!-- 通告栏 -->
		<view style="width: 90%;margin: 0 auto;">
			<uni-notice-bar show-icon scrollable :text="notice" />
		</view>

		<!-- 供需列表 -->
		<view class="flex-col group_5">
			<view class="flex-row justify-between items-center">
				<text class="font_3">最新供需</text>
				<view class="flex items-center group_6" @click="jumpLink('/pages/hall/hall',false)">
					<text class="font_4">更多</text>
					<image class="image_8 ml-6" :src="remoteUrl + 'right_icon.png'" />
				</view>
			</view>
			<view class="flex-col mt-10">
				<view class="flex-col list-item mt-10" v-for="(item,index) in need_list" :key="index"
					@click="toDetail(item.id)">
					<view class="flex-row justify-between items-center">
						<text class="font_5 row_1" style="width: 80%;">{{ item.title }}</text>
						<view class="flex">
							<view class="flex-col justify-start items-center text-wrapper view"
								v-if="item.type_id == 1">
								<text class="font_6">供</text>
							</view>
							<view class="flex-col justify-start items-center text-wrapper need"
								v-if="item.type_id == 2">
								<text class="font_6">需</text>
							</view>
							<view class="flex-col justify-start items-center text-wrapper not" v-if="item.status == 0">
								<text class="font_6">未解决</text>
							</view>
							<view class="flex-col justify-start items-center text-wrapper already"
								v-if="item.status == 1">
								<text class="font_6">已解决</text>
							</view>
						</view>
					</view>
					<view class="font_7 mt-14 row_1">
						<rich-text :nodes="item.content"></rich-text>
					</view>

					<!-- <text class="font_7 mt-14 row_1">{{ item.content }}</text> -->
					<view class="flex-row justify-between items-center mt-14">
						<view class="flex-row items-center">
							<image class="image_9" :src="ossUrl + item.user_avatar" />
							<text class="font_8 text_10">{{ item.user_name }}</text>
							<view class="section_10"></view>
							<text class="font_8 text_31">{{ item.user_company_name }}</text>
						</view>
						<text class="font_9 text_12">{{ item.updatetime }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 推荐课程 -->
		<view class="flex-col course_wrap mt-10">
			<view class="flex-row justify-between items-center">
				<text class="font_3">推荐课程</text>
				<view class="flex items-center course_title" @click="jumpLink('/pages/coures/coures',false)">
					<text class="font_4">更多</text>
					<image class="image_8 ml-6" :src="remoteUrl + 'right_icon.png'" />
				</view>
			</view>
			<view class="grid">
				<view class="grid-item" v-for="(item,index) in course_list"
					@click="jumpLink('/pages/pages-list/coures_detail/coures_detail?id='+item.id,true)">
					<image class="image_10" :src="ossUrl + item.image" />
					<view class="cont_item">
						<text class="font text_4 mt-10 row_2">{{ item.title }}</text>
						<view class="font_9 mt-6 flex justify-between">
							<text class="font_8">观看人数：{{ item.watch_number }}</text>
							{{ item.createtime }}
						</view>
						<view class="flex justify-between items-center mt-12">
							<view class="price">{{ item.price }}元</view>
							<view class="flex">
								<image @click.prevent="bindCollect(item.id,item.user_star,index)" class="collect_icon"
									v-if="item.user_star == 0" :src="remoteUrl + 'collect_n.png'" mode=""></image>
								<image @click.prevent="bindCollect(item.id,item.user_star,index)" class="collect_icon"
									v-if="item.user_star == 1" :src="remoteUrl + 'collect_y.png'" mode=""></image>
								<view class="flex-col justify-start items-center button" @click.prevent="toPay(item)">
									<text class="font_12">{{ item.user_buy == 0 ? '立即购买' : '已购买'}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		remoteUrl,
		ossUrl
	} from '@/utils/config.js'
	import {
		login
	} from '@/api/login.js'
	import {
		getBanner,
		new_Task,
		course_index
	} from '@/api/home.js'
	import {
		starCourse
	} from '@/api/coures.js'
	import {
		getNotice
	} from '@/api/forum.js'
	const app = getApp()
	export default {
		data() {
			return {
				remoteUrl: remoteUrl,
				ossUrl: ossUrl,
				statusBarHeight: app.globalData.navigator.statusBarHeight,
				navigatorBarHeight: app.globalData.navigator.navigatorBarHeight,
				search_text: '', // 搜索
				banner_list: [], // 轮播图
				need_list: [],
				course_list: [],
				notice: ''
			};
		},
		onLoad() {
			this.getBannerFun()
			this.getNoticeFun()
		},
		onShow() {
			this.getTask()
			this.getCourse()
		},
		onShareAppMessage(res) { //发送给朋友
			return {
				title: '小程序分享',
				imageUrl: '',
			}
		},
		methods: {
			// 获取banner
			getBannerFun() {
				getBanner().then(res => {
					if (res.code == 1) {
						this.banner_list = res.data
					}
				})
			},
			getNoticeFun() {
				getNotice().then(res => {
					if (res.code == 1) {
						this.notice = res.data[0].title
					}
				})
			},
			// 获取最新供需
			getTask() {
				new_Task().then(res => {
					if (res.code == 1) {
						this.need_list = res.data
					}
				})
			},
			// 获取推荐课程
			getCourse() {
				course_index().then(res => {
					if (res.code == 1) {
						this.course_list = res.data
					}
				})
			},
			// 跳转购买
			toPay(item) {
				if (!uni.getStorageSync('userInfo')) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
				if (item.user_buy == 1) return
				uni.navigateTo({
					url: '/pages/pages-list/couresOrder/couresOrder?detail=' + encodeURIComponent(JSON.stringify(
						item))
				})
			},
			jumpLink(link, type) {
				if (type) {
					if (!uni.getStorageSync('userInfo')) {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
					uni.navigateTo({
						url: link,
					});
				} else {
					uni.switchTab({
						url: link
					})
				}
			},
			// 收藏
			bindCollect(id, flag, index) {
				starCourse({
					id
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: flag == 0 ? '收藏成功' : '取消收藏成功',
							icon: 'none'
						})
						this.course_list[index].user_star = (flag == 0 ? 1 : 0)
					}
				})
			},
			focusinput(e) {
				uni.navigateTo({
					url: '/pages/search_page/search_page'
				})
			},
			toDetail(id) {
				if (!uni.getStorageSync('userInfo')) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
				uni.navigateTo({
					url: '/pages/pages-list/demand_detail/demand_detail?id=' + id
				})
			},
		},
	};
</script>

<style lang="scss" scoped>
	.home_wrap {
		.head_wrap {
			width: 100vw;
			height: 476rpx;
			background: linear-gradient(180deg, #1C41F1 0%, #1c41f100 100%);
			padding: 0 30rpx;

			.title {
				display: flex;
				align-items: center;
				color: #fff;
				font-size: 36rpx;
			}

			.search_item {
				width: 690rpx;
				height: 76rpx;
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				background: #fff;
				padding: 0 24rpx;
				border-radius: 58rpx;

				.search_icon {
					width: 28rpx;
					height: 28rpx;
					margin-right: 6rpx;
				}
			}
		}

		// 轮播图
		.swiper_wrap {
			width: 690rpx;
			height: 248rpx;
			margin: -210rpx auto 20rpx;

			.banner_img {
				width: 100%;
				height: 100%;
				border-radius: 20rpx;
			}
		}

		.equal-division {
			// margin-top: 10rpx;
			padding: 0 14rpx;

			.group_4 {
				flex: 1 1 180rpx;

				.image_7 {
					width: 112rpx;
					height: 112rpx;
				}

				.font_2 {
					font-size: 26rpx;
					color: #000000;
				}
			}

			.group_16 {
				padding: 8rpx 0 10.06rpx;
			}

			.equal-division-item_2 {
				padding: 8rpx 0 10rpx;
			}

			.group_15 {
				padding: 8rpx 0 10.4rpx;
			}
		}

		// 供需列表
		.group_5 {
			padding: 0 28rpx;

			.font_3 {
				font-size: 34rpx;
				font-weight: bold;
				color: #000000;
			}

			.group_6 {
				margin-right: 6rpx;

				.font_4 {
					font-size: 26rpx;
					color: #7b7b7b;
				}

				.image_8 {
					width: 12rpx;
					height: 24rpx;
				}
			}

			.list-item {
				padding: 22rpx 11.86rpx 16rpx 17.72rpx;
				background-color: #ffffff;
				border-radius: 12rpx;
				border: solid 2rpx #1c41f1;

				&:first-child {
					margin-top: 0;
				}

				.font_5 {
					font-size: 30rpx;
					color: #000000;
				}

				.text-wrapper {
					margin-right: 10rpx;

					.font_6 {
						font-size: 24rpx;
						color: #ffffff;
					}
				}

				.view {
					padding: 3rpx 7rpx;
					background-color: #70d042;
					border-radius: 4rpx;
				}

				.need {
					padding: 3rpx 7rpx;
					background-color: #f8d53a;
					border-radius: 4rpx;
				}

				.not {
					padding: 3rpx 7rpx;
					background-color: #999999;
					border-radius: 4rpx;
				}

				.already {
					padding: 3rpx 7rpx;
					border-radius: 4rpx;
					background: #1C41F1;
				}

				.font_7 {
					font-size: 24rpx;
					color: #000000;
				}

				.image_9 {
					border-radius: 20rpx;
					width: 40rpx;
					height: 40rpx;
				}

				.font_8 {
					font-size: 22rpx;
					color: #000000;
				}

				.text_10 {
					margin-left: 9.08rpx;
				}

				.section_10 {
					margin-left: 15.92rpx;
					background-color: #d9d9d9;
					width: 2rpx;
					height: 24rpx;
				}

				.text_31 {
					margin-left: 14.84rpx;
				}

				.font_9 {
					font-size: 20rpx;
					color: #000000;
				}

				.text_12 {
					margin-right: 4rpx;
				}
			}
		}

		// 推荐课程
		.course_wrap {
			padding: 0 28rpx;

			.font_3 {
				font-size: 34rpx;
				color: #000000;
				font-weight: bold;
			}

			.course_title {
				margin-right: 6rpx;

				.font_4 {
					font-size: 26rpx;
					color: #7b7b7b;
				}

				.image_8 {
					width: 12rpx;
					height: 24rpx;
				}
			}

			.grid {
				.grid-item {
					width: 690rpx;
					margin: 0 auto 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20.72rpx 11rpx 19rpx 13rpx;

					.image_10 {
						border-radius: 10rpx;
						width: 200rpx;
						height: 150rpx;
					}

					.cont_item {
						width: 68%;

						.font {
							font-size: 28rpx;
							color: #000000;
						}

						.font_9 {
							font-size: 20rpx;
							color: #000000;
						}

						.font_8 {
							font-size: 22rpx;
							color: #000000;
						}

						.price {
							color: #1c41f1;
							font-size: 28rpx;
						}

						.collect_icon {
							width: 40rpx;
							height: 40rpx;
							margin-right: 20rpx;
						}

						.button {
							padding: 3rpx 20rpx;
							background-color: #ffffff;
							border-radius: 54rpx;
							border: solid 1rpx #1c41f1;

							.font_12 {
								font-size: 22rpx;
								color: #1c41f1;
							}
						}
					}

				}
			}
		}
	}
</style>